// #ifdef  H5
import jweixin from 'weixin-js-sdk';
// #endif

export default {
    //判断是否在微信中  
    isWechat: function() {
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/micromessenger/i) == 'micromessenger') {
            return true;
        } else {
            return false;
        }
    },
    //初始化sdk配置  
    initJssdkShare: function(callback, url) {
		
		if(getApp().globalData.dev){
			var baseUrl=getApp().globalData.api_url_dev
		}else{
			var baseUrl=getApp().globalData.api_url_prod
		}
		
		uni.request({
		    url: baseUrl+'Third/getJsSign',
		    data: {
		        url: url
		    },
		    success: (res) => {
		        jweixin.config({
		            debug: res.data.data.debug,
		            appId: res.data.data.appId,
		            timestamp: res.data.data.timestamp,
		            nonceStr: res.data.data.nonceStr,
		            signature: res.data.data.signature,
		            jsApiList: res.data.data.jsApiList
		        });
		        //配置完成后，再执行分享等功能  
		        if (callback) {
		            callback(res.data);
		        }
		    }
		});
		
		
    },
    //在需要自定义分享的页面中调用  
    share: function(data) {
        let url = window.location.href;
        this.initJssdkShare(function(signData) {
            jweixin.ready(function() {
                var shareData = {
                    title: data.title,
                    desc:data.desc ,
                    link: url,
                    imgUrl:data.img,
                    success: function(res) {},
                    cancel: function(res) {}
                };
                jweixin.updateAppMessageShareData(shareData);
                jweixin.updateTimelineShareData(shareData);
            });
        }, url);
    },
}